package edu.pku.sei.modeler.uml.commands.statemachine;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.uml.BehaviorStateMachines.Pseudostate;
import edu.pku.sei.modeler.model.uml.BehaviorStateMachines.State;
import edu.pku.sei.modeler.model.uml.BehaviorStateMachines.StateMachine;
import edu.pku.sei.modeler.model.uml.BehaviorStateMachines.Transition;
import edu.pku.sei.modeler.uml.commands.ConnectionModelDeleteCommand;

public class DeleteTransitionCommand extends ConnectionModelDeleteCommand {

	public DeleteTransitionCommand(MetaModelElement connectionModel,
			MetaModelElement sourceModel, MetaModelElement targetModel) {
		super(connectionModel, sourceModel, targetModel);
	}
	
	@Override
	public boolean canExecute() {
		return true;
	}
	
	@Override
	public void execute() {
		Transition transition = (Transition) connectionModel;
		if(sourceModel != null){
			if(sourceModel instanceof State)
				((State)sourceModel).getOutgoing().remove(transition);
			else if(sourceModel instanceof Pseudostate)
				((Pseudostate)sourceModel).getOutgoing().remove(transition);
			else if (sourceModel instanceof StateMachine)
				((StateMachine)sourceModel).getOutgoing().remove(transition);
		}
		if(targetModel != null){
			if(targetModel instanceof State)
				((State)targetModel).getIncoming().remove(transition);
			else if(targetModel instanceof Pseudostate)
				((Pseudostate)targetModel).getIncoming().remove(transition);
			else if (sourceModel instanceof StateMachine)
				((StateMachine)sourceModel).getIncoming().remove(transition);
		}
		if(transition != null){
			transition.setContainer(null);
		}
	}
	
	@Override
	public void undo() {
		Transition transition = (Transition) connectionModel;
		if(sourceModel != null){
			if(sourceModel instanceof State)
				((State)sourceModel).getOutgoing().add(transition);
			else if(sourceModel instanceof Pseudostate)
				((Pseudostate)sourceModel).getOutgoing().add(transition);
			else if (sourceModel instanceof StateMachine)
				((StateMachine)sourceModel).getOutgoing().add(transition);
		}
		if(targetModel != null){
			if(targetModel instanceof State)
				((State)targetModel).getIncoming().add(transition);
			else if(targetModel instanceof Pseudostate)
				((Pseudostate)targetModel).getIncoming().add(transition);
			else if (sourceModel instanceof StateMachine)
				((StateMachine)sourceModel).getIncoming().add(transition);
		}
		if(transition != null){
			transition.setContainer(sourceModel);
		}
	}

}
